home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / libsrc / c / dos / fnsplit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-18  |  1.4 KB  |  77 lines

  1. /*   fnsplit.c replacement for Borland version pjbk */
  2.  
  3. #include <dir.h>
  4. #include <ctype.h>
  5. #include <string.h>
  6.  
  7. static char * max_ptr( char *p1, char *p2 )
  8. {
  9.     if ( p1 > p2 )
  10.         return p1;
  11.     else
  12.         return p2;
  13. }
  14.  
  15. int fnsplit (const char *path, char *drive, char *dir, 
  16.          char *name, char *ext)
  17. {
  18.   int flags = 0, len;
  19.   const char *pp, *pe;
  20.  
  21.   if ( drive ) *drive = '\0'; /* clear out any previous contents */
  22.   if ( dir ) *dir = '\0';
  23.   if ( name ) *name = '\0';
  24.   if ( ext ) *ext = '\0';
  25.  
  26.   pp = path;
  27.   if( isalpha( *pp) && (pp[1] == ':'))
  28.     {
  29.       flags |= DRIVE;
  30.       if ( drive )
  31.     {
  32.       strncpy( drive, pp, 2);
  33.       drive[2] = '\0';
  34.     }
  35.       pp += 2;
  36.     }
  37.   pe = max_ptr( strrchr( pp, '\\'),     /* find terminating \ */
  38.                 strrchr( pp, '/')  );   /* find terminating / */
  39.   if ( pe ) 
  40.     { 
  41.       flags |= DIRECTORY ;
  42.       pe++;
  43.       len = pe - pp;
  44.       if ( dir )
  45.     {
  46.       strncpy( dir, pp, len);
  47.       dir[len] = '\0';
  48.     }
  49.       pp = pe;
  50.     }
  51.   pe = strrchr( pp, '.'); /* find separator */
  52.   if ( pe )
  53.     {
  54.       flags |= EXTENSION;
  55.       if ( ext ) 
  56.     {
  57.       strcpy( ext, pe);
  58.     }
  59.     }
  60.   else 
  61.     pe = strchr( pp, '\0');
  62.   if ( pp != pe )
  63.     {
  64.       flags |= FILENAME;
  65.       len = pe - pp;
  66.       if ( name ) 
  67.     {
  68.       strncpy( name, pp, len);
  69.       name[len] = '\0';
  70.     }
  71.     }
  72.   if ( strchr( pp, '*') || strchr( pp, '?'))
  73.     flags |= WILDCARDS;
  74.   return flags;
  75.   
  76. }
  77.